using RuoVea.ExEnum;

namespace CodePen.Web.Controllers;

/// <summary>
/// Dashboard控制器
/// </summary>
public class DashboardController : Controller
{
    private readonly ContentService _contentService;
    private readonly CategoryService _categoryService;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="contentService"></param>
    /// <param name="categoryService"></param>
    public DashboardController(ContentService contentService, CategoryService categoryService)
    {
        _contentService = contentService;
        _categoryService = categoryService;
    }

    /// <summary>
    /// 首页
    /// </summary>
    /// <returns></returns>
    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 获取Dashboard统计数据
    /// </summary>
    /// <returns></returns>
    [HttpGet("dashboard/dashboardstats")]
    public async Task<RestfulResult> GetDashboardStats()
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK };
        try
        {
            // 使用新方法获取内容统计数据
            var contentStatsResult = await _contentService.GetDashboardStatsAsync();
            dynamic contentStats = contentStatsResult.Data;

            // 使用新方法获取分类统计数据
            var categoryStatsResult = await _categoryService.GetDashboardStatsAsync();
            dynamic categoryStats = categoryStatsResult.Data;

            // 处理最近添加内容的时间格式 - 使用反射安全获取属性
            var recentContentsObj = contentStats.GetType().GetProperty("recentContents").GetValue(contentStats);
            var recentContents = ((IEnumerable<object>)recentContentsObj)
                .Select(c =>
                {
                    var cType = c.GetType();
                    return new
                    {
                        id = cType.GetProperty("id").GetValue(c),
                        content = cType.GetProperty("content").GetValue(c),
                        category = cType.GetProperty("category").GetValue(c),
                        createTime = DateTime.Parse(cType.GetProperty("createTime").GetValue(c).ToString()).ToString("yyyy-MM-dd")
                    };
                }).ToList();

            // 获取分类内容分布 - 使用反射安全获取属性
            var categoryDistributionObj = contentStats.GetType().GetProperty("categoryDistribution").GetValue(contentStats);
            var categoryDistribution = ((IEnumerable<object>)categoryDistributionObj).ToList();

            // 构建返回数据 - 使用反射安全获取属性
            var totalContents = contentStats.GetType().GetProperty("totalContents").GetValue(contentStats);
            var activeContents = contentStats.GetType().GetProperty("activeContents").GetValue(contentStats);
            var disabledContents = contentStats.GetType().GetProperty("disabledContents").GetValue(contentStats);
            var totalCategories = categoryStats.GetType().GetProperty("totalCategories").GetValue(categoryStats);
            var activeCategories = categoryStats.GetType().GetProperty("activeCategories").GetValue(categoryStats);

            restfulResult.Data = new
            {
                contentCount = totalContents,
                activeContentCount = activeContents,
                disabledContentCount = disabledContents,
                categoryCount = totalCategories,
                activeCategoryCount = activeCategories,
                recentContents = recentContents,
                categoryDistribution = categoryDistribution
            };

            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }
}